const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const mysql = require('mysql')
const port = 3001


//创建数据库连接
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'city_traval_mt'
})

connection.connect()

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    next();
});

//test
// app.get('/', function (req, res) {
//   res.send('hello world')
// })

//热门推荐
app.get('/home_hot_list',  (req, res) => {
  connection.query(`select * from homepagelist;`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//限时抢购
app.get('/home_flash_sale',  (req, res) => {
  let id = req.query.id
  connection.query(`select * from hotellists where id=${id};`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//热门搜索
app.get('/home_hot_search',  (req, res) => {
  connection.query(`select * from hotsearchlist`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//酒店详情
app.get('/home_detail_list',  (req, res) => {
  let id = req.query.id
  connection.query(`select * from hotellists where id=${id};`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//酒店推荐
app.get('/hotel_list',  (req, res) => {
  let limit = req.query.limit
  connection.query(`select * from hotellists limit ${limit},9;`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//模糊搜索
app.get('/fuzzy_search_list',  (req, res) => {
  let txt = req.query.txt
  connection.query(`select * from hotellists where title like '%${txt}%'`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//城市搜索
app.get('/fuzzy_list_city',  (req, res) => {
  let txt = req.query.txt
  connection.query(`select * from citylist where city_type='${txt}'`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//注册
app.post('/register',  (req, res) => {
  let phone = req.body.phone
  let pwd = req.body.pwd
  let type = req.body.type
  let img = req.body.img
  console.log(req);
  connection.query(`insert into users (user_phone,user_password,user_card_type,user_img) values ("${phone}","${pwd}","${type}","${img}")`, (err, rows, fields) => {
    res.send({
      status: 200,
    })
  })
})

//手机号登录
app.post('/login_phone',  (req, res) => {
  let phone = req.body.phone
  connection.query(`select * from users where user_phone="${phone}"`, (err, rows, fields) => {
    if(rows.length > 0){
      res.send({
        status: 1,
        num: String(Math.floor(Math.random()*10))+Math.floor(Math.random()*10)+Math.floor(Math.random()*10)+Math.floor(Math.random()*10)
      })
    }else{
      res.send({
        status: 0,
      })
    }
  })
})

//密码登录
app.post('/login_pwd',  (req, res) => {
  let phone = req.body.phone
  let pwd = req.body.pwd
  connection.query(`select * from users where user_phone="${phone}" and user_password="${pwd}"`, (err, rows, fields) => {
    if(rows.length > 0){
      res.send({
        status: 1,
      })
    }else{
      res.send({
        status: 0,
      })
    }
  })
})

//获取用户信息
app.get('/mine_message',  (req, res) => {
  let phone = req.query.phone
  connection.query(`select * from users where user_phone="${phone}"`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})


app.listen(port,  () => {
  console.log(`Example app listening at http://localhost:${port}/`);
});
// connection.end() 